package com.starblink.post.contentdeleted.ui

import android.os.Bundle
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.base.activity.BaseTVMLoadingActivity
import com.starblink.android.basic.ext.ApolloValSetExt
import com.starblink.android.basic.ext.bindProductFeeds
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.android.basic.util.bus.bean.LikePostEvent
import com.starblink.android.common.view.post.SimplePostCardCell
import com.starblink.basic.route.RoutePage
import com.starblink.basic.style.title.OnTitleBarListener
import com.starblink.basic.style.title.TitleBar
import com.starblink.post.databinding.ActivityContentDeletedBinding
import com.starblink.rocketreserver.fragment.PostF

@Route(path = RoutePage.Post.PAGE_CONTENT_DELETED)
class ContentDeletedActivity :
    BaseTVMLoadingActivity<ActivityContentDeletedBinding, ContentDeletedVM>() {

    @Autowired(name = RoutePage.Post.ARGS_TIP)
    @JvmField
    var tip: String = ""

    var layoutManager: StaggeredGridLayoutManager? = null

    override fun initViewBinding(): ActivityContentDeletedBinding =
        ActivityContentDeletedBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (tip.isEmpty()) {
            tip = "This Content has been removed."
        }
        viewBinding.tvText1.text = tip

        viewBinding.titleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                finish()
            }
        })

        viewModel.getRecommend(initRefresh = true)

        layoutManager =
            viewBinding.recyclerView.bindProductFeeds(
                adapter,
                horGap = 9, withEdge = true,
            ) as StaggeredGridLayoutManager?

        viewBinding.refresher.setEnableRefresh(false)
        viewBinding.refresher.setOnLoadMoreListener {
            viewModel.getRecommend()
            viewBinding.refresher.finishLoadMore(5000)
        }

        //同步点赞数据
        FlowBus.with<LikePostEvent>(FlowConst.LIKE_POST_EVENT).register(this) {
            if (it.postId != null) {
                for ((index, post) in viewModel.mList.value!!.withIndex()) {
                    if (post != null && post is PostF && it.postId == post.id && it.like != post.liked) {
                        ApolloValSetExt.setApolloVal<PostF>(post, "liked", it.like)
                        ApolloValSetExt.setApolloVal<PostF>(post, "likeNumbers", it.likeCount)
                        adapter.notifyItemChanged(index)
                        break
                    }
                }
            }
        }
    }

    private val adapter: MutableAdapter by lazyOnNone {
        MutableAdapter(viewModel.mList.value!!).apply {
            addVhDelegate {
                SimplePostCardCell(
                    it,
                    viewModel.mList.value!!
                ) {
                    viewModel.getRecommend()
                }
            }
        }
    }


    override fun startObserve() {
        viewModel.mList.observe(this) {
            showStateSuccess()
            if (viewModel.mList.value != null && viewBinding.recyclerView.adapter == null) {
                viewBinding.recyclerView.adapter = adapter
            }
            if (viewModel.mList.value != null && viewModel.mList.value!!.isNotEmpty()) {
                if (viewModel.mList.value!!.size == 0) {
                    adapter.notifyDataSetChanged()
                } else {
                    adapter.notifyItemRangeChanged(
                        viewModel.pageSize * viewModel.pageNo,
                        viewModel.pageSize
                    )
                    viewModel.pageNo += 1
                }
            }
            if (viewBinding.refresher.isRefreshing) {
                viewBinding.refresher.finishRefresh()
            }
            if (viewBinding.refresher.isLoading) {
                viewBinding.refresher.finishLoadMore()
            }
        }

    }
}